package cn.wp.common.core.plugin.thread;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置
 * Created by chenyb on 2019/10/23
 */
@Configuration
// 启用 @Async 注解
@EnableAsync
public class ThreadPoolConfiguration {

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(100);//配置核心线程数
        executor.setMaxPoolSize(2000);//配置最大线程数
        // 配置队列大小
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("wp-threadpool-");//线程的名称前缀
        //Caller_runs:不在新线程中执行任务，而是在调用者所在线程执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

}
